/*
 * Copyright 2010 Kevin L'Huillier <klhuillier@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.lhuillier.pwsafe.io;


import org.lhuillier.pwsafe.model.Database;


/**
 * Reads an encrypted database and returns it in a decrypted form.
 * <p>
 * Based on the tag, this will use a reader appropriate for the database
 * version.
 * <p>
 * All methods decrypt the database. Some will also convert the raw binary
 * field data into the appropriate types using the default decoder. Other
 * methods will return the raw binary data to allow applications to handle
 * the decoding.
 */
public interface DbReader {
    /**
     * Creates a key appropriate for decrypting the database contained in the
     * binary data.
     * <p>
     * Different versions of Password Safe produce their stretched keys in
     * different ways. The binary data is provided so the correct implementation
     * can be selected.
     */
    StretchedKey prepareKey(byte[] data, String password) throws DbIoException;
    
    /**
     * Loads the database from a byte array containing an encrypted database.
     */
    Database load(byte[] data, StretchedKey key) throws DbIoException;
    
    /**
     * Partially loads the database from a byte array containing an encrypted
     * database.
     * <p>
     * Loading a raw database allows an application to handle decoding the
     * raw data of each field into a custom database object.
     */
    RawDb loadRaw(byte[] data, StretchedKey key) throws DbIoException;
}
